package weiy.top.modules.sku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import weiy.top.modules.sku.entity.WmsWareSku;
import weiy.top.modules.sku.mapper.WmsWareSkuMapper;
import weiy.top.modules.sku.service.IWmsWareSkuService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: wms_ware_sku
 * @Author: weihancheng
 * @Date:   2021-12-14
 * @Version: V1.0
 */
@Service
public class WmsWareSkuServiceImpl extends ServiceImpl<WmsWareSkuMapper, WmsWareSku> implements IWmsWareSkuService {

    @Override
    public void addStock(String skuId, String wareId, Integer skuNum) {
        // 判断是否有库存记录
        LambdaQueryWrapper<WmsWareSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmsWareSku::getSkuId, skuId);
        queryWrapper.eq(WmsWareSku::getWareId, wareId);
        List<WmsWareSku> entities = list(queryWrapper);

        if (entities == null || entities.size() == 0) {
            WmsWareSku skuEntity = new WmsWareSku();
            skuEntity.setSkuId(skuId);
            skuEntity.setStock(skuNum);
            skuEntity.setWareId(wareId);
            skuEntity.setStockLocked(0);

            //TODO 远程查询sku的名字，如果失败，整个事务无需回滚
            //1、自己catch异常
            //TODO 还可以用什么办法让异常出现以后不回滚？高级
//            try {
//                R info = productFeignService.info(skuId);
//                Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");
//
//                if(info.getCode() == 0){
//                    skuEntity.setSkuName((String) data.get("skuName"));
//                }
//            }catch (Exception e){
//
//            }
            save(skuEntity);
        } else {
//            wareSkuDao.addStock(skuId,wareId,skuNum);
        }
    }
}
